/**
 * @file main.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 测试高山流水的digest认证
 * @version 0.1
 * @date 2020-12-29
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#include <stdio.h>
#include <string>
#include "digcalc.h"
#include "md5.h"
#include "STL_MD5.h"
#include "string.h"

#include "STL_Digest.h"
void STL_main()
{
    std::string res = STL::STL_Digest::genDigest(
        "41010299111117999086",                       //std::string username,
        "1234",                                       //std::string password,
        "REGISTER",                                   //std::string method,
        "sip:41010299111117999086@192.168.1.86:5060", //std::string uri,
        "",                                           //std::string body,
        "4101029911",                                 //std::string realm,
        "1806613454",                                 //std::string nonce,
        "",                                           //std::string nc,
        "MD5",                                        //std::string algorithm,
        "",                                           //std::string cnonce,
        ""                                            //std::string qop
    );
    printf("res:%s\n", res.c_str());
};
/*
REGISTER sip:41010299112007999229@192.168.1.229:5060 SIP/2.0
Content-Length: 0
From: <sip:41010299111117999086@4101029911>;tag=975532346
To: <sip:41010299111117999086@4101029911>
Call-ID: zxw41010299111117999086-624100078@192.168.1.86
Contact: <sip:41010299111117999086@192.168.1.86:5060>
CSeq: 1966 REGISTER
Max-Forwards: 70
Via: SIP/2.0/UDP 192.168.1.86:5060;rport;branch=z9hG4bk258370691
User-Agent: PU
Authorization: Digest username="41010299111117999086",realm="4101029911",nonce="1806613454",uri="sip:41010299111117999086@192.168.1.86:5060",response="6c090dbcde4a19f4cad94808c1a322e2",algorithm=MD5
Expires: 3600
*/

int main(int argn, char **argv)
{
    STL_main();
    /*
    void DigestCalcHA1(
    IN char * pszAlg,
    IN char * pszUserName,
    IN char * pszRealm,
    IN char * pszPassword,
    IN char * pszNonce,
    IN char * pszCNonce,
    OUT HASHHEX SessionKey
    )
     */
    char pszAlg[] = "MD5";
    char pszUserName[] = "41010299111117999086";
    char pszRealm[] = "4101029911";
    char pszPassword[] = "1234";
    char pszNonce[] = "1806613454";
    char pszCNonce[] = "";
    // md5(41010299111117999086:4101029911:1234) = a25b9207c02f195bd12e711541cfb832

    /*
    void DigestCalcResponse(
    IN HASHHEX HA1,           /* H(A1) 
    IN char * pszNonce,       /* nonce from server 
    IN char * pszNonceCount,  /* 8 hex digits 
    IN char * pszCNonce,      /* client nonce 
    IN char * pszQop,         /* qop-value: "", "auth", "auth-int" 
    IN char * pszMethod,      /* method from the request 
    IN char * pszDigestUri,   /* requested URL 
    IN HASHHEX HEntity,       /* H(entity body) if qop="auth-int" 
    OUT HASHHEX Response      /* request-digest or response-digest 
    )
     */
    // char pszNonce[] = "1806613454";
    char pszNonceCount[] = "";
    // char pszCNonce[] = "";
    char pszQop[] = "";
    char pszMethod[] = "REGISTER";
    char pszDigestUri[] = "sip:41010299111117999086@192.168.1.86:5060";

    // md5(REGISTER:sip:41010299111117999086@192.168.1.86:5060)=7933163119cb2a18105a03326d6f460e

    //md5(a25b9207c02f195bd12e711541cfb832:1806613454:7933163119cb2a18105a03326d6f460e) = 6c090dbcde4a19f4cad94808c1a322e2

    // result
    char pszResponse[] = "6c090dbcde4a19f4cad94808c1a322e2";

    HASHHEX HA1;
    HASHHEX Response;

    {
        printf("res[1]:%s\n",md5("123456").c_str());
    }

    {
        unsigned char src[] = "123456";
        struct MD5Context Md5Ctx;
        HASH HA1;
        MD5Init(&Md5Ctx);
        MD5Update(&Md5Ctx, src, 6);
        MD5Final((unsigned char *)HA1, &Md5Ctx);
        printf("res[2]:");
        for (size_t i = 0; i < 16; i++)
        {
            printf("%02x",HA1[i] & 0xFF);
        }
        printf("\n");
        //	e10adc3949ba59abbe56e057f20f883e
    }

    DigestCalcHA1(pszAlg, pszUserName, pszRealm, pszPassword, pszNonce, pszCNonce, HA1);
    printf("HA1=%s\n", HA1);

    DigestCalcResponse(HA1, pszNonce, pszNonceCount, pszCNonce, pszQop, pszMethod, pszDigestUri, NULL, Response);

    printf("pszResponse=%s\n", pszResponse);
    printf("Response=%s\n", Response);
    return 0;
}